<?php
namespace app\api\controller\service;

use app\admin\model\user\Target;
use app\api\model\UserCustomerVisitLog;
use app\api\model\wanlshop\Auth;
use app\api\model\wanlshop\Order;
use app\common\controller\Api;
use app\common\model\CompetitionOrders;
use app\common\model\FishingGroundOrders;

class Business extends Api{
    protected $noNeedRight ='*';
    public function dashboard(){
        $data = Target::get(['uid' => $this->auth->id]);

        $tempArr = [
            'sum' => 0,
            'complete' => 0
        ];
        $gmv = $tempArr;
        $gmv['sum'] = $data['gmv'] ?? 0;

        $customer = $tempArr;
        $customer['sum'] = $data['new_customer'] ?? 0;
        $customer['complete'] = Auth::where(['service_id' => $this->auth->id])->count();

        $order = $tempArr;
        $order['sum'] = $data['order_count'] ?? 0;
        $shop_order_count = Order::where(['service_id' => $this->auth->id])->count();
        $fish_group_order_count = FishingGroundOrders::where(['service_id' => $this->auth->id])->count();
        $fish_competition_order_count = CompetitionOrders::where(['service_id' => $this->auth->id])->count();
        $order['complete'] = $shop_order_count+$fish_group_order_count+$fish_competition_order_count;


        $rebuy = $tempArr;
        $rebuy['sum'] = $data['rebuy_count'] ?? 0;
        $rebuy['complete'] = Order::where(['service_id' => $this->auth->id])
            ->field('count(user_id) as buy_count')
            ->having('buy_count > 1')
            ->group('store_id')
            ->where('dealtime' ,'>',0)
            ->count();

        $current_day_visit_count = UserCustomerVisitLog::where('service_id',$this->auth->id)->whereTime('createtime','d')->count();
        $current_month_customer_count = Auth::where(['service_id' => $this->auth->id])->whereTime('createtime','d')->count();

        $this->success('',[
            'gmv' => $gmv,
            'new_customer' => $customer,
            'order' => $order,
            'rebuy' => $rebuy,
            'current_day_visit_count' => $current_day_visit_count,
            'current_month_customer_count' => $current_month_customer_count
        ]);
    }
}